/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.core.util.StringUtils;
import net.dgg.dggcloud.service.common.base.SessionManager;
import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.cms.UserSettingDao;
import net.dgg.dggcloud.service.jrcrm.entity.cms.UserSetting;
import net.dgg.dggcloud.service.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;

/**
 * 用户设置业务处理
 *
 * @ClassName: UserSettingService
 * @Author: 周潮
 * @Date: 2017年11月02 15:32:27
 * @Version V1.0
 */

@Service
public class UserSettingService extends BaseService {

    @Autowired
    private UserSettingDao userSettingDao;
    @Autowired
    private UserService userService;

    /**
     * 根据条件查询员工设置列表
     *
     * @param map
     * @return
     */
    public List<Map> queryUserSettingListByParams(Map map) {
        return userSettingDao.queryUserSettingListByParams(map);
    }

    /**
     * 根据条件查询员工设置列表
     *
     * @param map
     * @return
     */
    public Integer countUserSettingListByParams(Map map) {
        return userSettingDao.countUserSettingListByParams(map);
    }

    /**
     * 根据id查询单个员工设置信息
     *
     * @param id
     * @return
     */
    public UserSetting queryUserSettingById(Long id) {
        return super.repository.get(UserSetting.class, id);
    }

    /**
     * 根据外呼号查询单个员工设置信息
     *
     * @param map
     * @return
     */
    public List<Map> findUserBySeatNumber(Map map) {
        return userSettingDao.findUserBySeatNumber(map);
    }

    /**
     * 根据外呼号或电信外呼号查询单个员工设置信息
     *
     * @param map
     * @return
     */
    public List<Map> findUserByCallNum(Map map) {
        return userSettingDao.findUserByCallNum(map);
    }


    /**
     * 根据条件查询单个员工设置信息
     *
     * @param map
     * @return
     */
    public Map queryUserSettingByParams(Map map) {
        return userSettingDao.queryUserSettingByParams(map);
    }

    /**
     * 更新员工设置
     *
     * @param userSetting
     */
    public void updateUserSettingByParams(UserSetting userSetting) {
        try {

            this.repository.saveOrUpdate(userSetting);
        } catch (Exception e) {
            throw new RuntimeException("fail");
        }
    }

    /**
     * 删除员工设置
     *
     * @param ids
     */
    public void deleteUserSettingByParams(String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            userSettingDao.deleteByIdHiber(UserSetting.class, Long.parseLong(ids[i]));
        }
    }

    /**
     * 更新用户接单权限
     *
     * @param uids  用户ID集合
     * @param type  1接单，0不可接单
     * @param loger 当前登录人
     */
    public void updateReceive(String uids, Integer type, User loger) {
        String[] arryIds = uids.split("\\|");
        for (int i = 0; i < arryIds.length; i++) {
            Long uid = Long.parseLong(arryIds[i]);
            UserSetting us = super.repository.get(UserSetting.class, uid);
            //如果员工设置为空，则新建员工设置
            if (null == us) {
                us = new UserSetting();
                us.setId(uid);
                us.setCreateUser(loger);
                us.setIsReceive(type);
            } else {
                us.setUpdaterUser(loger);
                us.setIsReceive(type);
            }
            this.repository.saveOrUpdate(us);
        }
    }

    /**
     * 更新用户月接单量
     *
     * @param map uids 用户ID集合</br>
     *            levelZl 助理商务月接单量</br>
     *            levelCj 初级商务月接单量 </br>
     *            levelZj 中级商务月接单量 </br>
     *            levelGj 高级商务月接单量 </br>
     *            allotWarning 预警</br>
     *            loger 当前登录人/br>
     */
    public void updateMonthNum(Map map) {
        String[] arryIds = (String[]) map.get("uids");
        User loger = (User) map.get("loger");
        Integer allotWarning = (Integer) map.get("allotWarning");
        for (int i = 0; i < arryIds.length; i++) {
            Long uid = Long.parseLong(arryIds[i]);
            UserSetting us = super.repository.get(UserSetting.class, uid);
            //如果员工设置为空，则新建员工设置
            if (null == us) {
                us = new UserSetting();
                us.setId(uid);
                us.setCreateUser(loger);
                us.setIsReceive(0);
                us.setMonthNum(0);
                us.setVipNum(0);
            } else {
                us.setUpdaterUser(loger);
                String uLevel = us.getLevel();
                if (!StringUtils.isEmpty(uLevel)) {
                    us.setMonthNum((Integer) map.get(uLevel));
                }
            }
            us.setAllotWarning(allotWarning);
            this.repository.saveOrUpdate(us);
        }
    }

    /**
     * 更新用户Vip量
     *
     * @param map uids 用户ID集合</br>
     *            workYear1 1年以下员工Vip量</br>
     *            workYear1 1年到2年员工Vip量 </br>
     *            workYear1 2年以上员工Vip量 </br>
     *            loger 当前登录人/br>
     */
    public void updateVipNum(Map map) {
        String[] arryIds = (String[]) map.get("uids");
        User loger = (User) map.get("loger");
        Integer workYear1 = (Integer) map.get("workYear1");
        Integer workYear2 = (Integer) map.get("workYear2");
        Integer workYear3 = (Integer) map.get("workYear3");
        for (int i = 0; i < arryIds.length; i++) {
            Long uid = Long.parseLong(arryIds[i]);
            UserSetting us = super.repository.get(UserSetting.class, uid);
            User user = userService.getUserById(uid);
            //如果员工设置为空，则新建员工设置
            if (null == us) {
                us = new UserSetting();
                us.setId(uid);
                us.setCreateUser(loger);
                us.setIsReceive(0);
                us.setMonthNum(0);
            } else {
                us.setUpdaterUser(loger);
            }
            Integer userWorkYear = null == user.getWorkAge() ? 0 : user.getWorkAge();
            if (userWorkYear < 1) {
                us.setVipNum(workYear1);
            } else if (userWorkYear >= 1 && userWorkYear < 2) {
                us.setVipNum(workYear2);
            } else if (userWorkYear >= 2) {
                us.setVipNum(workYear3);
            } else {
                us.setVipNum(0);
            }
            this.repository.saveOrUpdate(us);
        }
    }

    public void importUserLevel(List<List<String>> rowList, User loger) {
        rowList.remove(0);//移除第一列标题
        for (int i = 0; i < rowList.size(); i++) {
            List<String> row = rowList.get(i);
            String loginName = StringUtils.isNotEmpty(row.get(0)) ? row.get(0) : "";
            String uLevel = StringUtils.isNotEmpty(row.get(2)) ? row.get(2) : "";
            if (StringUtils.isNotEmpty(loginName)) {
                User user = userService.getUserByLoginName(loginName);
                if (null != user) {
                    UserSetting us = super.repository.get(UserSetting.class, user.getId());
                    //如果员工设置为空，则新建员工设置
                    if (null == us) {
                        us = new UserSetting();
                        us.setId(user.getId());
                        us.setCreateUser(loger);
                        us.setIsReceive(0);
                        us.setMonthNum(0);
                        us.setVipNum(0);
                    } else {
                        us.setUpdaterUser(loger);
                    }
                    us.setLevel(uLevel);
                    this.repository.saveOrUpdate(us);
                }
            }
        }
    }

    /**
     * 设置后台人员业务类型
     *
     * @param ids
     * @param businessTypeCode 业务类型
     * @param policyNum        接单量
     */
    public void updateBusinessType(String ids, String businessTypeCode, Integer policyNum) {
        User user = SessionManager.getCurrentUser();
        Assert.notNull(user, "当前用户获取失败，请刷新重新登录！");
        String[] idsStrArray = ids.split(",");
        for (int i = 0; i < idsStrArray.length; i++) {
            Long usid = Long.parseLong(idsStrArray[i]);
            UserSetting us = super.repository.get(UserSetting.class, usid);
            us.setBusinessTypeCode(businessTypeCode);
            us.setPolicyNum(policyNum == null ? null : policyNum);
            this.repository.saveOrUpdate(us);
        }
    }

    /**
     * 设置后台流程人员的级别
     *
     * @param id
     * @param businessTypeCode 业务类型
     * @param policyNum        接单量
     * @param flowUserRank     级别
     */
    public void updateBackstage(String id, String businessTypeCode, Integer policyNum, String flowUserRank) {
        User user = SessionManager.getCurrentUser();
        Assert.notNull(user, "当前用户获取失败，请刷新重新登录！");
        UserSetting us = super.repository.get(UserSetting.class, Long.parseLong(id));
        Assert.notNull(us, "没有查询到选择的数据信息！");
        us.setPolicyNum(policyNum == null ? null : policyNum);
        us.setBusinessTypeCode(businessTypeCode);
        us.setFlowUserRank(flowUserRank);
        this.repository.saveOrUpdate(us);
    }
}
